package com.adinnet.api.schoolWebsite.controller;


import com.adinnet.api.address.model.Address;
import com.adinnet.api.schoolWebsite.model.SchoolWebsite;
import com.adinnet.api.schoolWebsite.service.SchoolWebsiteService;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResp;
import com.adinnet.core.exception.BizException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 校区网点 前端控制器
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-04
 */
@RestController
@RequestMapping("/api/schoolWebsite")
public class SchoolWebsiteController {


    @Autowired
    private SchoolWebsiteService schoolWebsiteService;

    /**
     * 分校区网点列表
     *
     * @return
     */
    @GetMapping("list")
    public PageResp schoolList(Integer limit, Integer page, String searchFields, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (null == limit) {
                return new PageResp("limit不能为空");
            }
            if (null == page) {
                return new PageResp("page不能为空");
            }
            IPage<SchoolWebsite> websiteIPage = schoolWebsiteService.schoolList(limit, page, searchFields);
            if (null == websiteIPage || CollectionUtils.isEmpty(websiteIPage.getRecords())) {
                return new PageResp(Lists.newArrayList());
            }
            return new PageResp(websiteIPage.getRecords(), websiteIPage.getTotal());
        } catch (BizException e) {
            return new PageResp(e.getMessage());
        } catch (Exception e) {
            return new PageResp("查询分校区网点列表失败!");
        }
    }

    /**
     * 分校区网点详情
     *
     * @return
     */
    @GetMapping("list/details")
    public JsonResult schoolDetails(Integer id, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (null == id) {
                return JsonResult.error("分校区网点id不能为空");
            }
            List<String> images= schoolWebsiteService.schoolDetails(id);
            if (CollectionUtils.isEmpty(images)) {
                return JsonResult.ok().put("data", Lists.newArrayList());
            }
            return JsonResult.ok().put("data", images);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("查询分校区网点详情失败!");
        }
    }

    /**
     * 分校区网点地区列表
     *
     * @return
     */
    @GetMapping("list/address")
    public JsonResult schoolAddressList(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<Address> addresses= schoolWebsiteService.schoolAddressList();
            if (CollectionUtils.isEmpty(addresses)) {
                return JsonResult.ok().put("data", Lists.newArrayList());
            }
            return JsonResult.ok().put("data", addresses);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("查询分校区网点地区列表失败!");
        }
    }
}
